package com.jhd.jiangxi.recycle.utils;


import android.content.Context;
import android.content.res.Configuration;
import android.widget.TextView;

/**
 * Created by wuzd on 2018/5/17.
 */

public class ScreenUtils {
    private static float density = -1.0F;
    private static int widthPixels = -1;
    private static int heightPixels = -1;
    private static final float ZERO = 0.0F;

    private ScreenUtils() {
    }

    public static float getDensity(Context context) {
        if (null == context) {
            return 0.0F;
        } else {
            if (density <= 0.0F) {
                density = context.getApplicationContext().getResources().getDisplayMetrics().density;
            }

            return density;
        }
    }

    public static int dip2px(Context context, float dpValue) {
        return null == context ? 0 : (int) (dpValue * getDensity(context) + 0.5F);
    }

    public static int px2dip(Context context, float pxValue) {
        return null == context ? 0 : (int) (pxValue / getDensity(context) + 0.5F);
    }

    public static int getScreenWidth(Context context) {
        if (null == context) {
            return 0;
        } else {
            if (widthPixels <= 0) {
                widthPixels = context.getApplicationContext().getResources().getDisplayMetrics().widthPixels;
            }

            return widthPixels;
        }
    }

    public static int getScreenHeight(Context context) {
        if (null == context) {
            return 0;
        } else {
            if (heightPixels <= 0) {
                heightPixels = context.getApplicationContext().getResources().getDisplayMetrics().heightPixels;
            }

            return heightPixels;
        }
    }

    public static void setDefaultTextSize(Context context, TextView textView) {
        if (null != context) {
            Configuration config = context.getResources().getConfiguration();
            textView.setTextSize(0, textView.getTextSize() / config.fontScale);
        }
    }
}
